package cn.funeralobjects.asms.service.impl;

import cn.funeralobjects.asms.entity.ESmsSendRecord;
import cn.funeralobjects.asms.entity.ESmsSign;
import cn.funeralobjects.asms.entity.ESmsTemplate;
import cn.funeralobjects.asms.model.BasicInfo;
import cn.funeralobjects.asms.model.SmsSendRecord;
import cn.funeralobjects.asms.repository.ESmsSendRecordRepository;
import cn.funeralobjects.asms.service.SmsSendRecordService;
import cn.funeralobjects.common.jpa.service.JpaAddService;
import cn.funeralobjects.common.jpa.service.JpaEntityService;
import cn.funeralobjects.common.jpa.service.JpaNameService;
import cn.funeralobjects.common.repository.EntityRepository;
import cn.funeralobjects.common.repository.NameRepository;
import cn.funeralobjects.common.service.annotataion.CommonRollback;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import cn.funeralobjects.util.annotation.ArgNotEmpty;
import cn.funeralobjects.util.annotation.AssertArg;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author FuneralObjects
 * Create date: 2020/5/31 10:51 PM
 */
@CommonService
public class SmsSendRecordServiceImpl implements SmsSendRecordService, JpaAddService<SmsSendRecord, ESmsSendRecord, Integer>, JpaEntityService<ESmsSendRecord, Integer>, JpaNameService<ESmsSendRecord, Integer> {

    @Resource
    private ESmsSendRecordRepository eSmsSendRecordRepository;

    @Override
    public CrudRepository<ESmsSendRecord, Integer> addRepository() {
        return eSmsSendRecordRepository;
    }

    @Override
    public void checkConflict(SmsSendRecord smsSendRecord) throws DataConflictException {
    }

    @Override
    public ESmsSendRecord toEntity(SmsSendRecord smsSendRecord) {
        return smsSendRecord == null ? null : new ESmsSendRecord()
                .setSmsSendParam(smsSendRecord.getSmsSendParam())
                .setSmsBizId(smsSendRecord.getSmsBizId())
                .setSmsOutId(smsSendRecord.getSmsOutId())
                .setSmsErrCode(smsSendRecord.getSmsErrCode())
                .setSmsErrMessage(smsSendRecord.getSmsErrMessage())
                .setSmsSendTelephone(smsSendRecord.getSmsSendTelephone())
                .setSmsUpExtendCode(smsSendRecord.getSmsUpExtendCode())
                .setSign(new ESmsSign().setId(smsSendRecord.getSmsSignId()))
                .setTemplate(new ESmsTemplate().setId(smsSendRecord.getSmsTemplateId()));
    }

    @Override
    public EntityRepository<ESmsSendRecord, Integer> entityRepository() {
        return eSmsSendRecordRepository;
    }


    @Override
    public NameRepository<ESmsSendRecord, Integer> nameRepository() {
        return eSmsSendRecordRepository;
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(SmsSendRecord smsSendRecord) {
        Assert.argAssert(smsSendRecord, "smsSendRecord");
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }

    @CommonRollback
    @AssertArg
    @Override
    public List<ESmsSendRecord> addInBatch(@ArgNotEmpty List<SmsSendRecord> sendRecords) {
        return sendRecords.stream()
                .filter(Objects::nonNull)
                .map(this::add)
                .collect(Collectors.toList());
    }
}
